const fileService = require("../service/file.service");

const { updateAvatarUrlById } = require("../service/goods.service");

class FileController {
  async saveAvatarInfo(ctx, next) {
    // console.log(222);
    console.log(1, ctx.req.file);
    const { filename, mimetype, size } = ctx.req.file;
    // const { id } = ctx.user;
    const goods_id = ctx.request.params.goods_id;
    // console.log(goods_id);
    // 2.将图像信息数据保存到数据库中
    const result = await fileService.createAvatar(
      filename,
      mimetype,
      size,
      +goods_id
    );
    // 3.将图片地址保存到goods表总
    const avatarUrl = `http://localhost:8001/goods/avatar/${goods_id}`;
    // const avatarUrl = `http://82.157.176.4:8005/goods/avatar/${goods_id}`;
    await updateAvatarUrlById(avatarUrl, goods_id);
    console.log(result);
    ctx.body = "添加图片成功";
  }
}

module.exports = new FileController();
